$(function(){
    $(".myaccount_li").on("mouseenter",function(){
        $(".myaccount_cont").show();
    }).on("mouseleave",function(){
        $(".myaccount_cont").hide();
    })

    //购物车数量加减
    $(".goods_list").on("click",".reduce_g",function(){
        var count = $(this).parents(".selectGoods").find(".input_goods input").val();
        count--;
        if(count <=1 ){
            count = 1;
        }
        // $(".input_goods").find("input").val(count);
        // $(".weight").find("span").html($(".input_goods").find("input").val()*2.5);
        // $(".goodsMsg .total").find("span").html(($(".input_goods").find("input").val()*89.90).toFixed(2));

        $(this).parents(".selectGoods").find(".input_goods input").val(count);
        $(this).parents(".selectGoods").find(".weight span").html(count*2.5);
        let price = $(this).parents(".selectGoods").find(".unit-price span").text();
        console.log(price);
        $(this).parents(".selectGoods").find(".goodsMsg .total span").html((count*price).toFixed(2));

        $.ajax({
            url:"/cart/modify",
            type:"post",
            data:{
                //jq的data方法，获取自定义属性的值
                cid:$(this).parents(".selectGoods").data("cid"),
                cnum:count
            }
        })
        Alltotal();
    })
    
    $(".goods_list").on("click",".plus_g",function(){
        var count = $(this).parents(".selectGoods").find(".input_goods input").val();
        count++;
        if(count <=1 ){
            count = 1;
        }
        $(this).parents(".selectGoods").find(".input_goods input").val(count);
        //重量加减
        $(this).parents(".selectGoods").find(".weight span").html(count*2.5);
        let price = $(this).parents(".selectGoods").find(".unit-price span").text();
        $(this).parents(".selectGoods").find(".goodsMsg .total span").html((count*price).toFixed(2));

        $.ajax({
            url:"/cart/modify",
            type:"post",
            data:{
                //jq的data方法，获取自定义属性的值
                cid:$(this).parents(".selectGoods").data("cid"),
                cnum:count
            }
        })

        Alltotal()
    })

    //删除单件商品
    $("option") 

    //查询
    $.ajax({
        url:"./cart/query",
        type:"get",
        data:{uId:JSON.parse(localStorage.getItem("userinfo"))[0].u_id}
    }).then(function(res){
        layer.msg(res.msg);
        render(res.data);
        // console.log(res);
    })
    function render(list){
        let  strHtml = '';
        list.forEach(el=>{
            strHtml += `<div data-cid=${el.c_id} class="selectGoods clear">`
            strHtml+=`<div class="fl">
            <input class='fl cheackIpt'  style="margin: 20px 5px 0 20px;" type="checkbox" checked>
            <a><img class="fl" src=${el.c_img} alt=""></a>
            <div class="goodsName fl fs12">${el.c_name}</div>
            </div>`
            strHtml += `<div class="goodsMsg fr">
            <div class="unit-price _msgwidth">
                ￥
                <span>${Number(el.c_price).toFixed(2)}</span>
            </div>
            <div class="count _msgwidth">
                <div class="oper_goods reduce_g"></div>
                <div class="input_goods">
                    <input type="number" value=${el.c_num} name="count">
                </div>
                <div class="plus_g oper_goods"></div>
            </div>
            <div class="weight _msgwidth">
                <span>2.5</span>
            </div>
            <div class="total _msgwidth">
                ￥<span>${el.c_total}</span>
            </div>
            <div class="option _msgwidth">
                <button>收藏</button>
                <button class="del">删除</button>
            </div>
            </div>`
            strHtml+=`</div>`
        });
        $(".goods_list").html(strHtml);
        Alltotal();
    }

    $(".goods_list").on("click",".del",function(){
        let cId = $(this).parents(".selectGoods").data("cid");
        let _this = this;
        layer.confirm("您确定要删除吗？",{
            btn:["确定","再想想"]
        },function(index){
            $.ajax({
                url:"/cart/delete",
                type:"post",
                data:{cId}
            }).done(function(res){
                layer.msg(res.msg);
                if(res.status == 200){
                    $(_this).parents(".selectGoods").remove();
                    $.ajax({
                        url:"./cart/query",
                        type:"get",
                        data:{uId:JSON.parse(localStorage.getItem("userinfo"))[0].u_id}
                    }).then(function(res){
                        layer.msg(res.msg);
                        render(res.data);
                    })
                }
            })
            layer.close(index);
        },function(){

        })
    })
    let flag = false;
    $(".selectAll").on("click",function(){
        flag = !flag
        $(".selectGoods input:checkbox").prop("checked",$(this).prop("checked"));
        $(".title :checkbox").prop("checked",$(this).prop("checked"));
    //    if(flag){
        // document.querySelectorAll(".selectGoods .cheackIpt").forEach(function(el){
        //     el.className = "fl cheackIpt current"
        // })
    //    }else{
        // document.querySelectorAll(".selectGoods .cheackIpt").forEach(function(el){
        //     el.className = "fl cheackIpt"
        // })
    //    }
    if($(this).prop("checked")){
        document.querySelectorAll(".selectGoods .cheackIpt").forEach(function(el){
            el.className = "fl cheackIpt"
        })
    }else{
        document.querySelectorAll(".selectGoods .cheackIpt").forEach(function(el){
            el.className = "fl cheackIpt current"
        })
    }
        Alltotal()
    })

    $(".goods_list").on("click",".selectGoods :checkbox",function(){
        let cbxCount = $(".selectGoods input:checkbox").length;
        let checkedCount = $(".selectGoods :checkbox:checked").length;
        $(".selectAll").prop("checked",cbxCount === checkedCount);
        $(".title :checkbox").prop("checked",$(this).prop("checked"));
    })

    $(".selectGoods").find(".cheackIpt").each(function(index,el){
        el.temp = true
    })
    $(".goods_list").on("click",".cheackIpt",function(){
        this.temp = !this.temp;
        if(this.temp==false){
            $(this).removeClass("current")
        }
       else{
           $(this).addClass("current")
       }
       Alltotal()
     })






    function Alltotal(){
        let totalMoney = 0;
        let totalNum = 0;
        let totalWeight = 0;
        $(".selectGoods").find(".input_goods input").each(function(index,el){
            totalNum+=Number($(el).val());
        })
        $(".goods_list .selectGoods .total").find("span").each(function(index,el){
            totalMoney += Number($(el).text().trim())
        })
        $(".goods_list .selectGoods").find(".weight span").each(function(index,el){
            totalWeight+=Number($(el).text().trim())
        })
        $(".goods_list").find(".current").each(function(index,el){
            totalMoney -= Number($(el).parents(".selectGoods").find(".total span").html())
        })
    
        // $(".selectGoods").find(".cheackIpt").each(function(index,el){
        //     $(el).prop("checked",false);
        // })
        $(".order_right").find(".totalprice").text(totalMoney.toFixed(2));
        $(".order_right").find(".count span").text(totalNum);
        $(".order_right").find(".shouldPay").text((totalMoney.toFixed(2)-$(".youhui").text()).toFixed(2));
        $(".order_right").find(".goodsweight span").text(totalWeight);
    }

   
})